package com.my.spvideo.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;

/**
 * 控制层基类
 * @author 承
 *
 */
public class BasicController {
	
	protected final void to403(HttpServletRequest request,HttpServletResponse response,String msg) throws ServletException, IOException{
		response.setStatus(403);
		request.setAttribute("msg", msg);
		request.getRequestDispatcher("/error403.jsp").forward(request, response);
	}
	
	protected final void to404(HttpServletRequest request,HttpServletResponse response,String msg) throws ServletException, IOException{
		response.setStatus(404);
		request.setAttribute("msg", msg);
		request.getRequestDispatcher("/error404.jsp").forward(request, response);
	}
	
	protected final void to405(HttpServletRequest request,HttpServletResponse response,String msg) throws ServletException, IOException{
		response.setStatus(405);
		request.setAttribute("msg", msg);
		request.getRequestDispatcher("/error405.jsp").forward(request, response);
	}
	
	protected final void to500(HttpServletRequest request,HttpServletResponse response,String msg) throws ServletException, IOException{
		response.setStatus(500);
		request.setAttribute("msg", msg);
		request.getRequestDispatcher("/error500.jsp").forward(request, response);
	}
	
	protected final void writeJSON(HttpServletResponse response,JSONObject json) throws IOException{
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter pw = response.getWriter();
		pw.print(json.toJSONString());
		pw.flush();
		pw.close();
	}
	
	protected final void writeJSONStr(HttpServletResponse response,String jsonStr) throws IOException{
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter pw = response.getWriter();
		pw.print(jsonStr);
		pw.flush();
		pw.close();
	}
	
	protected final void writeStr(HttpServletResponse response,String str) throws IOException{
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;UTF-8");
		PrintWriter pw = response.getWriter();
		pw.print(str);
		pw.flush();
		pw.close();
	}
	
	protected final void writeInt(HttpServletResponse response,int i) throws IOException{
		PrintWriter pw = response.getWriter();
		pw.print(i);
		pw.flush();
		pw.close();
	}
	
	protected final void writeDouble(HttpServletResponse response,double b) throws IOException{
		PrintWriter pw = response.getWriter();
		pw.print(b);
		pw.flush();
		pw.close();
	}
	
	protected final void writeDouble(HttpServletResponse response,float f) throws IOException{
		PrintWriter pw = response.getWriter();
		pw.print(f);
		pw.flush();
		pw.close();
	}
	
	protected final void writeDouble(HttpServletResponse response,short s) throws IOException{
		PrintWriter pw = response.getWriter();
		pw.print(s);
		pw.flush();
		pw.close();
	}
	
	protected final void writeChar(HttpServletResponse response,char c) throws IOException{
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw = response.getWriter();
		pw.print(c);
		pw.flush();
		pw.close();
	}
	
}
